<div class="eject_con">
  <div id="warning" class="alert alert-error"></div>
  <form id="post_form" method="post" >
    <input type="hidden" name="form_submit" value="ok" />
    <dl>
      <dt><i class="required">*</i>实体店铺名称<?php echo $lang['nc_colon'];?></dt>
      <dd>
        <input class="text w200" type="text" name="name_info_t" value="" />
        <p class="hint">不同地址建议使用不同名称以示区别，如“山西面馆(水游城店)”。</p>
      </dd>
    </dl>
    <dl>
      <dt><i class="required">*</i>详细地址<?php echo $lang['nc_colon'];?></dt>
      <dd>
        <input class="text w200" type="text" name="address_info_t" id="address_info" value=""  />
        <p class="hint">为了准确定位建议地址加上所在城区名字，如“红桥区大丰路18号水游城”。</p>
      </dd>
    </dl>
    <dl>
      <dt>联系电话<?php echo $lang['nc_colon'];?></dt>
      <dd>
        <input class="text w200" type="text" name="phone_info_t" value=""  />
      </dd>
    </dl>
    <dl>
      <dt>公交信息<?php echo $lang['nc_colon'];?></dt>
      <dd>
        <textarea name="bus_info_t" rows="2" class="textarea w300"></textarea>
      </dd>
    </dl>
      <dl>
          <dt>地图位置<?php echo $lang['nc_colon'];?></dt>
          <dd>
              <input type="hidden" name="baidu_lng_t" value="">
              <input type="hidden" name="baidu_lat_t" value="">
              <input type="hidden" name="baidu_province_t" value="">
              <input type="hidden" name="baidu_city_t" value="">
              <input type="hidden" name="baidu_district_t" value="">
              <input type="hidden" name="baidu_street_t" value="">
              <button type="button" id="open_map">选择</button>
          </dd>
      </dl>
      <div id="baidu_map" style="height:200px;border:1px solid gray;display: none;"></div>
    <div class="bottom">
        <label class="submit-border"><input type="submit" id="submit_button" class="submit" value="<?php echo $lang['nc_ok'];?>" /></label>
    </div>
  </form>

</div>

<script type="text/javascript">

$(function(){

    $('input[name="name_info_t"]').val($('input[name="name_info"]').val());
    $('input[name="address_info_t"]').val($('input[name="address_info"]').val());
    $('input[name="phone_info_t"]').val($('input[name="phone_info"]').val());
    $('textarea[name="bus_info_t"]').val($('textarea[name="bus_info"]').val());
    $('input[name="baidu_lng_t"]').val($('input[name="baidu_lng"]').val());
    $('input[name="baidu_lat_t"]').val($('input[name="baidu_lat"]').val());

    $('input[name="baidu_province_t"]').val($('input[name="baidu_province"]').val());
    $('input[name="baidu_city_t"]').val($('input[name="baidu_city"]').val());
    $('input[name="baidu_district_t"]').val($('input[name="baidu_district"]').val());
    $('input[name="baidu_street_t"]').val($('input[name="baidu_street"]').val());

    $('#post_form').validate({
        errorLabelContainer: $('#warning'),
        invalidHandler: function(form, validator) {
            $('#warning').show();
        },
    	submitHandler:function(form){
            $('input[name="name_info"]').val($('input[name="name_info_t"]').val());
            $('input[name="address_info"]').val($('input[name="address_info_t"]').val());
            $('input[name="phone_info"]').val($('input[name="phone_info_t"]').val());
            $('textarea[name="bus_info"]').val($('textarea[name="bus_info_t"]').val());
            $('input[name="baidu_lng"]').val($('input[name="baidu_lng_t"]').val());
            $('input[name="baidu_lat"]').val($('input[name="baidu_lat_t"]').val());

            $('input[name="baidu_province"]').val($('input[name="baidu_province_t"]').val());
            $('input[name="baidu_city"]').val($('input[name="baidu_city_t"]').val());
            $('input[name="baidu_district"]').val($('input[name="baidu_district_t"]').val());
            $('input[name="baidu_street"]').val($('input[name="baidu_street_t"]').val());
            $('#address_info_span').html($('input[name="address_info_t"]').val());
            $('#post_form').parents('#fwin_map_edit').find('.dialog_close_button').click();
    	},
        rules : {
            name_info_t : {
                required : true
            },
            address_info_t : {
                required   : true
            },
            baidu_lng_t : {
                required   : true
            },
            baidu_lat_t : {
                required   : true
            }
        },
        messages : {
            name_info_t : {
                required : '<i class="icon-exclamation-sign"></i>实体店铺名称不能为空'
            },
            address_info_t  : {
                required   : '<i class="icon-exclamation-sign"></i>详细地址不能为空'
            },
            baidu_lng_t : {
                required : '<i class="icon-exclamation-sign"></i>地图坐标不能为空'
            },
            baidu_lat_t  : {
                required   : '<i class="icon-exclamation-sign"></i>请使用鼠标点击地图选择坐标'
            }

        }
    });

    var map = new BMap.Map("baidu_map");
    var getData = new BMap.Geocoder();
    map.clearOverlays();//清空原来的标注
    var baidu_lng_t = $('input[name="baidu_lng_t"]').val();
    var baidu_lat_t = $('input[name="baidu_lat_t"]').val();
    if(baidu_lng_t&&baidu_lat_t){
        var point = new BMap.Point(baidu_lng_t, baidu_lat_t);
        map.centerAndZoom(point, 15);
        var marker = new BMap.Marker(point);  // 创建标注，为要查询的地方对应的经纬度
        map.addOverlay(marker);
    }

    $('#open_map').click(function(){

        map.enableScrollWheelZoom(true);

        var localSearch = new BMap.LocalSearch(map);
        localSearch.setSearchCompleteCallback(function (searchResult) {
            var poi = searchResult.getPoi(0);
            if(poi==undefined)
            {
                alert("您输入的地址无法获取经位置请查正后重试，或者手动选择位置");
            }
            else{
                map.centerAndZoom(poi.point, 12);
            }

        });

        var address_info = $('input[name="address_info_t"]').val();
        localSearch.search(address_info);

        map.addEventListener("click", function(e){
            $('input[name="baidu_lng_t"]').val(e.point.lng);
            $('input[name="baidu_lat_t"]').val(e.point.lat);
            get_location(e.point.lng,e.point.lat);
            map.clearOverlays();    //删除所有marker
            var marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));  // 创建标注，为要查询的地方对应的经纬度
            map.addOverlay(marker);
        });

        $('#baidu_map').css('display','block');
    });

    function get_location(lng,lat){
        var point = new BMap.Point(lng, lat);
        getData.getLocation(point, function(rs){
            var xiangxi = rs.addressComponents;
            $('input[name="baidu_province_t"]').val(xiangxi.province);
            $('input[name="baidu_city_t"]').val(xiangxi.city);
            $('input[name="baidu_district_t"]').val(xiangxi.district);
            $('input[name="baidu_street_t"]').val(xiangxi.street);
        });
    }
});

</script>
